package com.sirui.x.learnIo.netty;

import com.sirui.x.learnIo.netty.handle.BootHandleIn;
import com.sirui.x.learnIo.netty.handle.ServeHandleIn1;
import com.sirui.x.learnIo.netty.handle.ServeHandleIn2;
import com.sirui.x.learnIo.netty.handle.ServeHandleOut1;
import com.sirui.x.learnIo.netty.handle.ServeHandleOut2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServerWrapper {

	
	public static void main(String[] args) throws InterruptedException {
		EventLoopGroup acceptEvent = new NioEventLoopGroup(1);
		EventLoopGroup readEvent = new NioEventLoopGroup();
		
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		try {
			serverBootstrap
				.channel(NioServerSocketChannel.class)
				.group(acceptEvent, readEvent)
				.localAddress(4200)
				.handler(new BootHandleIn())
				.childHandler(new ChannelInitializer<SocketChannel>() {
					
					protected void initChannel(SocketChannel ch) throws Exception {
						ChannelPipeline pipeline = ch.pipeline();
						pipeline.addLast(new ServeHandleIn1());
						pipeline.addLast(new ServeHandleIn2());
						pipeline.addLast(new ServeHandleOut2());
						pipeline.addLast(new ServeHandleOut1());
					};
				});
			ChannelFuture future = serverBootstrap.bind().sync();
			future.channel().closeFuture().sync();
		} finally {
			acceptEvent.shutdownGracefully();
			readEvent.shutdownGracefully();
		}
	}
}
